/**
 * Copyright (c) 2012 Nokia Corporation.
 * All rights reserved.
 *
 * For the applicable distribution terms see the license text file included in
 * the distribution.
 */

#ifndef NETWORKACCESS_H
#define NETWORKACCESS_H

#include <QtDeclarative/QDeclarativeNetworkAccessManagerFactory>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

// Custom user agent for showing login screen correctly.
const QString CustomUserAgent("Mozilla/5.0 (Unknown; U; Windows NT 6.1) " \
                              "en-US) AppleWebKit/533.3 (KHTML, like Gecko) " \
                              "Qt/4.7.4 Safari/533.3");



/*!
  \class NetworkAccessManager
  \brief Custom network access manager for customizing user agent in the header.
*/

class NetworkAccessManager : public QNetworkAccessManager
{
public:

    NetworkAccessManager(QObject *parent = 0)
        : QNetworkAccessManager(parent){
    }

    QNetworkReply * createRequest(Operation op,
                                  const QNetworkRequest &request,
                                  QIODevice *outgoingData) {
        QNetworkRequest customRequest(request);
        customRequest.setRawHeader("User-Agent", CustomUserAgent.toAscii());
        return QNetworkAccessManager::createRequest(op,
                                                    customRequest,
                                                    outgoingData);
    }
};


/*!
  \class NetworkAccessManagerFactory
  \brief Custom network access manager factory class for creating customized
         network access manager for QML.
*/


class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
{
public:
    QNetworkAccessManager * create(QObject *parent = 0) {
        return new NetworkAccessManager(parent);
    }
};

#endif // NETWORKACCESS_H
